<template>
    <div>
        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button-group>
                    <el-button icon="el-icon-circle-plus-outline" size="small"
                        @click="selectOrgDailog(userRoleForm.pkGroup,userRoleForm.pkRole)">关联组织</el-button>
                    <el-button icon="el-icon-remove-outline" size="small" @click="deleteHandle()">删除组织</el-button>
                </el-button-group>
            </div>
            <div>
                <el-button size="small" round icon="el-icon-refresh" @click="getRoleOrgList()"></el-button>
            </div>
        </div>

        <el-table :data="userroleList" size="small" min-height="300" style="width: 100%" highlight-current-row
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" header-align="center" align="center" width="50">
            </el-table-column>
            <el-table-column property="pkRole" label="角色编号"></el-table-column>
            <el-table-column property="roleName" label="角色标识"></el-table-column>
            <el-table-column property="roleDescription" label="角色描述"></el-table-column>
            <el-table-column property="groupName" label="所属集团"></el-table-column>
            <el-table-column property="orgName" label="所属组织"></el-table-column>
            <el-table-column property="enabledate" label="有效期"></el-table-column>
            <el-table-column property="disabledate" label="失效期"></el-table-column>

            <div slot="empty">
                <div class="avue-empty">
                    <div class="avue-empty__image" style="height: 50px;"><img
                            src=""
                            alt=""></div>
                    <p class="avue-empty__desc">暂无数据</p>
                </div>
            </div>
        </el-table>

        <!--选择组织弹出框  -->
        <org-dailog v-if="orgDailogVisible" ref="orgDailog" @refreshDataList="getRoleOrgList"></org-dailog>
    </div>

</template>
<script>
    import OrgDailog from './dailog/org-dailog'
    import * as userRoleService from '@/api/upms/userrole'
    import * as orgService from '@/api/upms/org'
    import * as roleOrgService from '@/api/upms/roleorg'
    export default {
        data() {
            return {
                userRoleForm: {
                    pkRole: '',
                    pkGroup: '',
                },
                userroleList: [],
                orgDailogVisible: false,
                multipleSelection: []
            }
        },
        components: {
            OrgDailog
        },
        methods: {
            init(pkGroup,roleId) {
                if (roleId) {
                    this.userRoleForm.pkRole = roleId;
                    this.userRoleForm.pkGroup = pkGroup;
                    this.getRoleOrgList()
                }
            },
            // 默认加载用户角色授权列表
            getRoleOrgList() {
                roleOrgService.getroleorglist(this.userRoleForm).then(data => {
                    this.userroleList = data
                })
            },
            //多选
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 弹框选择用户
            selectOrgDailog(pkGroup,roleId) {
                this.orgDailogVisible = true
                this.$nextTick(() => {
                    this.$refs.orgDailog.init(pkGroup,roleId)
                })
            },
            // 删除
            deleteHandle(id) {
                var ids = id ? [id] : this.multipleSelection.map(item => {
                    return item.pkRoleOrg
                })
                var names = this.multipleSelection.map(item => {
                    return item.orgName
                })

                if (ids.length > 0) {
                    this.$confirm(`确定对[${names.join(',')}]进行删除操作?`, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        roleOrgService.delBatchroleorg(ids).then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.getRoleOrgList()
                                }
                            })
                        })
                    }).catch(() => {})
                }else{
                    this.$message.warning("请至少选择一项！")
                }

            }
        }
    }
</script>